package com.gbr.client.view;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.ListView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.Priority;
import javafx.stage.Stage;

import com.gbr.application.model.Capture;
import com.gbr.application.model.Frame;
import com.gbr.client.presentation.IDesktopPresentation;

public class DesktopGui extends Stage implements PropertyChangeListener {
    private IDesktopPresentation    presentation        = null;
    private MenuBuilder             menuBuilder         = null;
    private CaptureListDelegate     captureListDelegate = null;
    private FrameListDelegate       frameListDelegate   = null;
    private PlayerDelegate          playerDelegate      = null;

    public DesktopGui(IDesktopPresentation prez) {
        presentation = prez;
        menuBuilder = new MenuBuilder(presentation, this);
        initComponent();
    }

    private void initComponent() {
        // delegates
        captureListDelegate = new CaptureListDelegate(presentation);
        frameListDelegate = new FrameListDelegate(presentation);
        playerDelegate = new PlayerDelegate(presentation);

        Pane root = buildRoot();
        // addToolbar(root);
        Scene scene = new Scene(root, 800, 600);
        setTitle("Desktop Recorder");
        setScene(scene);

        presentation.addPropertyChangeListener(this);
    }

    private Pane buildRoot() {
        BorderPane layout = new BorderPane();
        layout.setPadding(new Insets(5));

        // set top with menu
        layout.setTop(menuBuilder.build());

        // set left with Capture List
        Pane leftPane = buildLeftPane();
        layout.setLeft(leftPane);

        // set center with Video Pane
        Pane centerPane = buildCenterPane();
        layout.setCenter(centerPane);

        // set bottom with Frame List
        Pane bottomPane = buildBottomPane();
        layout.setBottom(bottomPane);

        return layout;
    }

    private Pane buildBottomPane() {
        HBox hbox = new HBox();
        hbox.setPrefHeight(175);
        hbox.setPadding(new Insets(5));
        ListView<Frame> listView = frameListDelegate.getList();
        HBox.setHgrow(listView, Priority.ALWAYS);
        hbox.getChildren().add(listView);

        return hbox;
    }

    private Pane buildCenterPane() {
        return playerDelegate.getPlayer();
    }

    private Pane buildLeftPane() {
        HBox hbox = new HBox();
        hbox.setPadding(new Insets(5));
        ListView<Capture> listView = captureListDelegate.getList();
        hbox.getChildren().add(listView);

        return hbox;
    }

    @Override
    public void propertyChange(final PropertyChangeEvent evt) {
    }
}
